using System;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.UI.MailContentEditing
{
    internal class HtmlWIContentEditor : HtmlContentEditor
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public WorkItemType WorkItemType
        {
            get { return this.m_workItem; }
            set
            {
                this.m_workItem = value;

                this.WorkItemRootMI.DropDownItems.Clear();

                if (this.m_workItem != null)
                {
                    foreach (FieldDefinition field in this.m_workItem.FieldDefinitions)
                    {
                        ToolStripMenuItem item = new ToolStripMenuItem(field.Name);
                        item.Click += new EventHandler(OnInsertWirkItemField);
                        this.WorkItemRootMI.DropDownItems.Add(item);
                    }
                }
            }
        }
        private WorkItemType m_workItem = null;

        private ToolStripMenuItem WorkItemRootMI
        {
            get
            {
                if (this.m_wiRootMI == null)
                    this.m_wiRootMI = new ToolStripMenuItem("Insert WorkItem field");

                return this.m_wiRootMI;
            }
        }
        private ToolStripMenuItem m_wiRootMI = null;

        public HtmlWIContentEditor()
            : base()
        {
            this.ContextMenu.Items.Add(this.WorkItemRootMI);
        }

        private void OnInsertWirkItemField(object sender, EventArgs args)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            if (menuItem == null) return;

            FieldDefinition field = null;

            try
            {
                field = this.WorkItemType.FieldDefinitions[menuItem.Text];

                if (field != null)
                {
                    string insertedText = string.Format(Resources.FIELD_CONTENT_PATTERN, field.Name);
                    if (insertedText != null) this.Paste(insertedText);
                }
            }
            catch
            { }
        }

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HtmlWIContentEditor));
            this.SuspendLayout();
            // 
            // HtmlWIContentEditor
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.BodyHtml = null;
            this.BodyText = null;
            this.DocumentText = resources.GetString("$this.DocumentText");
            this.EditorBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.EditorForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.FontSize = FontSize.Three;
            this.Name = "HtmlWIContentEditor";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}
